package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootQd;
import org.footballmanager.pandafootball.system.domain.FootTeam;
import org.footballmanager.pandafootball.system.form.impl.FootQdSearchForm;
import org.footballmanager.pandafootball.system.mapper.FootQdMapper;
import org.footballmanager.pandafootball.system.service.FootQdService;
import org.footballmanager.pandafootball.system.service.FootTeamService;
import org.footballmanager.pandafootball.system.service.PhotoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;

@Slf4j
@Service
public class FootQdServiceImpl extends ServiceImpl<FootQd> implements FootQdService {
    @Autowired
    private FootQdMapper footQdMapper;
	@Autowired
	private FootTeamService footTeamService;
	@Autowired
	private PhotoService photoService;

	@Override
	@Transactional
	public int delete(Serializable id, HttpServletRequest request) {
		FootQd t = this.get(id);
		// 球队图片实体的删除
		if(t.getLogo() != null){
			photoService.delete(t.getLogo(), request);
		}
		if(t.getSlt() != null){
			photoService.delete(t.getSlt(), request);
		}
		List<FootTeam> list = footTeamService.findTeamByQdId(t.getId());
		for(FootTeam team: list){
			team.setQdId(null);
			footTeamService.updateNotNull(team, request);
		}
		log.info("删除球队:" + t.toString());
		return super.delete(id, request);
	}

	@Override
	public FootQd findByName(String name) {
		Example example = new Example(FootQd.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("qdbm", name);
		return this.findByExample(example);
	}

    /**
     * 翻页方法
     *
     * @param footQdSearchForm 条件
     * @return 列表
     */
    @Override
    public List<FootQd> findall(FootQdSearchForm footQdSearchForm) {
        return footQdMapper.findall(footQdSearchForm);
    }

	/**
	 * 根据主键获取记录
	 * @param id 主键
	 * @return 记录
	 */
	@Override
	public FootQd get(Serializable id) {
		return footQdMapper.findById(id);
	}

    /**
     * 查询
     *
     * @param entityname       实体类名
     * @param map              参数
     * @param footQdSearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootQdSearchForm footQdSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footQdSearchForm);
        Page<FootQd> page = PageHelper.startPage(footQdSearchForm.getPageIndex(), footQdSearchForm.getPageSize());
        List<FootQd> list = this.findall(footQdSearchForm);
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 删除所有
     *
     * @param footQdSearchForm 条件
     * @param request          请求
     */
    @Override
    public void deleteAll(FootQdSearchForm footQdSearchForm, HttpServletRequest request) {
        if(footQdSearchForm.getSelectIds() != null){
            String[] result = footQdSearchForm.getSelectIds().split(Constants.COMMA);
            for(String id: result){
                this.delete(Long.parseLong(id), request);
            }
        }
    }
}
